// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("../leftMenu/leftMenu");
    var topMenu = require("../topMenu/topMenu");
    var router = require("../hash/pageRouter");
    var dialog = require("dialog");
    var footer = require("../footer/footer_main");
    var validate = require("validate");
    var databind = require("databind");
    var html_add = require("./logisticsCompany_add.html");
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
        
    var init = function(num){
    	leftMenu.init(num);
    	topMenu.init(2);
    	
    	footer.init();  	
    	$("#footer").removeClass("none");
    	
    	$(".footer").removeClass("none");
    	$(".table-section").removeClass("none");
    
		$("#leftMenu5").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu5 .leftMenu li:first-child").addClass("leftMenu-open");
		$(".logisticsCompany").addClass("leftMenu-active");
    	getLogisticsCompanyList();
		initEvent();
		router.init();
    };
    
    var html_delete = require("../client/logisticsCompany_delete.html");
	//var html_details = require("../client/logisticsCompany_details.html");
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		 //搜索按钮
        $(".table-search-button").on("click",function(){
            pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
            getLogisticsCompanyList();
        });
        
        $(".memberGradeAduitList").unbind("click").click(function(){
			router.route("client/memberGradeaudit.html");
		});
        
        //点击新增物流公司
        $(".table-head").on("click",".button-submit",function(){
			$("#popdialog").html(html_add);
			var add_logisticsCompany_dialog = $("#add_logisticsCompany_dialog");
				dialog.showDialog(add_logisticsCompany_dialog, function() {});
				createLogisticsCompany();
				$(".companyAccount").unbind("change").change(function(){
				var companyAccount =	$(this).val();
				var myregex = new RegExp(" /^[0-9a-zA-Z]*$/g"); // 创建正则表达式 
				if (companyAccount.match(/[^A-Za-z0-9]/ig)){
					base.alert(2,"登录账户只允许字母和数字！");
					return ;
				} 
			});
        });
        
       
    };
    
    
    var createLogisticsCompany = function(){
    	$("#sure").on("click",function(){
    				var companyAccount =	$(".companyAccount").val();
					var myregex = new RegExp(" /^[0-9a-zA-Z]*$/g"); // 创建正则表达式 
					if (companyAccount.match(/[^A-Za-z0-9]/ig)){
							base.alert(2,"登录账户只允许字母和数字！");
							return ;
						} 
    		databind.submitDom($(".dialogContent"),function(){
    			base.alert(1,"创建物流公司信息成功!默认密码为：123456");
    			dialog.dialogClear($(".dialogContent"));
    			getLogisticsCompanyList();
    		});
    	});
    	
    }
    var disableLogisticsCompany = function(companyId){
    	var delete_logisticsCompany_dialog = $("#delete_logisticsCompany_dialog");
    	dialog.showDialog(delete_logisticsCompany_dialog, function() {});
    	$(".footerBtn").on("click", "#sure_delete", function() {
    	base.fnAjax({
            action: "disableLogisticsCompany.json",
            oData: {
            
            	companyId: companyId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	dialog.hideDialog($("#delete_logisticsCompany_dialog"));
                	getLogisticsCompanyList();
                } else {
                    base.alert(2,"物流公司删除失败");
                }
            }
        });
    	});
    }
    
    var getLogisticsCompanyList = function(){
    	var body = base.parseForm($("#searchLogisticsCompany"));
		body.createDateTimeBegin = body.createDateTimeBegin.split("-").join("");
		body.createDateTimeEnd = body.createDateTimeEnd.split("-").join("");
		base.fnAjax({
            action: "inquireLogisticsCompanyList.json",
            oData: {
            	
            	pageIndex: pageIndex,
                pageSize: pageSize,
                createDateTimeBegin:body.createDateTimeBegin,
                createDateTimeEnd:body.createDateTimeEnd,
                companyAccount:body.companyAccount,
                companyName:body.companyName,
                reportState:body.reportState,
                effective:body.effective
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	var tpl = $("#logisticsCompanyListTpl").html();
                    var html = juicer(tpl, data);
                    $("#logisticsCompanyListBody").html(html);
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl(
                    	$("#pagenation"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            getLogisticsCompanyList();
                    });
                    
                     //点击详情
        $("#logisticsCompanyListBody").on("click",".table_details_link",function(){
           var companyId = [];
           var $this = $(this).parents("tr.tr_style");
		   companyId.push($this.attr("companyId"));
			router.route("client/logisticsCompany_details.html?companyId="+companyId,true);
        });
		
         //点击删除
        $("#logisticsCompanyListBody").on("click",".table_delete_btn",function(){
        	$("#popdialog").html(html_delete);
           var companyId = [];
           var $this = $(this).parents("tr.tr_style");
		   companyId.push($this.attr("companyId"));
           disableLogisticsCompany(companyId);
        });
     
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
        });
    }
	
    return {
        init:init
    };
});